home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_10.lha / 7_10 / test6.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  4KB  |  128 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. *ident    "@(#)ctrans:demo/task/regtest.C    1.1" */
  6. include <debug.h>    /* DELETE */
  7. **************************************************************************
  8.         Copyright (c) 1984 AT&T
  9.             All Rights Reserved      
  10.  
  11. THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
  12.  
  13. The copyright notice above does not evidence any       
  14. actual or intended publication of such source code.
  15.  
  16. ****************************************************************************/
  17. include <process.h>
  18.  
  19. * non-trivial process example:
  20. Make a set of processes which pass an object round between themselves.
  21. Each process but the last creates the next process.
  22. Each process gets an object from the head of one queue, and puts
  23. the object on the tail of another queue.
  24. Each process quits after getting the object MAX_CYCLES times.
  25. Each process uses lots of registers that must be preserved across a switch.
  26. /
  27. onst int NTASKS = 1;
  28. onst int MAX_CYCLES = 5;
  29.  
  30. oid
  31. irty()
  32.  
  33. register    i, j, k, l, m, z;
  34. i = -1; j = -2; k = -3; l = -4; m = -5; z = -6;
  35.  
  36.  
  37. truct pc : process {                // derive a class from process
  38. queue_tail *t;
  39. queue_head *h;
  40. char *n;
  41.        pc(char*nn, queue_tail*nt, queue_head*nh) // process is not intended to be used directly
  42. : (nn)
  43.     t = nt; h = nh; n = nn;
  44. }
  45. long main();
  46. ;
  47.  
  48. ong pc::main()                    // process body serves as
  49.  
  50. register int    i, j, k, l, m, z;
  51. i = *n + 11; j = *n + 12; k = *n + 13;
  52. l = *n + 14; m = *n + 15; z = *n + 16;
  53.        printf("new pc(%s)\n",n);
  54. if (*n < 'a'+ NTASKS) {
  55.     char*    c = new char[2]; c[0] = *n + 1; c[1] = '\0';
  56.     queue_tail*    qt = new queue_tail;
  57.     pc*    next = new pc(c, t, qt->head());
  58.     next->exec();
  59.     t = qt;
  60. } else {
  61.     printf("%s: here we go\n", n);
  62.     t->put(new process_object);
  63. }
  64. if (this_process() != this) abort();
  65. if (i != *n + 11 || j != *n + 12 || k != *n + 13 ||
  66.     l != *n + 14 || m != *n + 15 || z != *n + 16) {
  67.     printf("pc:  lost regs! i==%d, j==%d, k==%d, l==%d, m==%d, z==%d\n", i, j, k, l, m, z);
  68.     printf("vals should be: i: %d, j: %d, k: %d, l: %d, m: %d, z: %d\n", *n + 11, *n + 12, *n + 13, *n + 14, *n + 15, *n + 16);
  69.     abort();
  70. }
  71.        for (int cycles = 0; cycles < MAX_CYCLES; cycles++) {
  72.     process_object* p = h->get();
  73.     dirty();
  74.     printf("process %s\n",n);
  75.     t->put(p);
  76. }
  77. printf("process %s: done.\n", n);
  78. exit(0);        // Always end process constructors with resultis.
  79.             // Behavior of those using return or running
  80.             // off the end of the function is undefined.
  81.             // Alternatively, use an infinite loop in
  82.             // constructor body.
  83.  
  84.  
  85. ain()
  86.  
  87. register int    i, j, k, l, m, z;
  88. queue_head* hh = new queue_head;
  89. queue_tail* t = hh->tail();
  90. queue_head* h;
  91.  
  92. printf("main\n");
  93.  
  94. char* n = "a"; // make a one letter process name
  95. i = *n + 1; j = *n + 2; k = *n + 3;
  96. l = *n + 4; m = *n + 5; z = *n + 6;
  97.  
  98. h = new queue_head;
  99. (new pc(n,t,h))->exec();
  100. printf("main()'s loop\n");
  101. if (i != *n + 1 || j != *n + 2 || k != *n + 3 ||
  102.     l != *n + 4 || m != *n + 5 || z != *n + 6) {
  103.     printf("main:  lost regs! i==%d, j==%d, k==%d, l==%d, m==%d, z==%d\n", i, j, k, l, m, z);
  104.     printf("values should be: i: %d, j: %d, k: %d, l: %d, m: %d, z: %d\n", *n + 1, *n + 2, *n + 3, *n + 4, *n + 5, *n + 6);
  105.     abort();
  106. }
  107. t = h->tail();
  108.        for (int cycles = 0; cycles < MAX_CYCLES; cycles++) {
  109.     process_object* p = hh->get();
  110.     if (i != *n + 1 || j != *n + 2 || k != *n + 3 ||
  111.         l != *n + 4 || m != *n + 5 || z != *n + 6) {
  112.         printf("main (after get):  lost regs! i==%d, j==%d, k==%d, l==%d, m==%d, z==%d\n", i, j, k, l, m, z);
  113.         printf("values should be:             i: %d, j: %d, k: %d, l: %d, m: %d, z: %d\n", *n + 1, *n + 2, *n + 3, *n + 4, *n + 5, *n + 6);
  114.         abort();
  115.     }
  116.     printf("main process\n");
  117.     t->put(p);
  118. }
  119. printf("main process: done.\n", n);
  120. main_process()->exit(0);        // main is a process too; it must also
  121.                 // end with exit (to allow any
  122.                 // remaining processes to run, otherwise
  123.                 // the whole process would exit).
  124.    printf("should not get here\n");
  125.    return 0;
  126.  
  127.